package Ep02_LinkedList.Offer_35;



import Ep02_LinkedList.Node;

import java.util.HashMap;

/**
 * @author Jimmy Zhan WORKSTATION
 * @since 2023/7/13 13:58
 * 说明：复制随机链表
 *      使用 map做原链表与复制链表之间的映射关系
 */

public class Mine {
    public Node copyRandomList(Node head) {
        if (head == null) return head;
        HashMap<Node, Node> map = new HashMap<>();
        Node cur = head;
        while(cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }

        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }

}
